package com.ce.pms.charge.web;


import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.charge.service.IPrePayOffsetService;
import com.ce.pms.charge.service.IPrePaySchService;
import com.ce.pms.charge.service.ISubAccountService;
import com.ce.pms.charge.service.ITransactionService;
import com.ce.pms.charge.vo.ChargeDetailSearchVO;
import com.ce.pms.charge.vo.PrePayAccountVO;
import com.ce.pms.charge.vo.PrePayOffsetSearchVO;
import com.ce.pms.charge.vo.PrePayOffsetTransVO;
import com.ce.pms.charge.vo.PrePayOffsetVO;
import com.ce.pms.charge.vo.PrePaySchVO;
import com.ce.pms.charge.vo.TransactionVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.exception.UILayerException;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;
import com.sitechasia.webx.core.support.Page;

public class PreparePayAction extends PmsBaseAction{

	/**
	 * 功能描述: 加载预交款交易明细页面 <br>
	 * @param houseId  房屋ID
	 * @param accountId  帐户Id
	 * @param modelType 请求源模块(是哪个模块来请求，如果modelType=forChange，则是费用模块来调用此方法、页面跳转到chargePreparePayDetailList.jsp页面)
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-9-23  <br>
	 * Why:     <br>
	 */
	public String housePreparePayDetailList(
			@Read(key="keyword")Long houseId,
			@Read(key="accountId")@Out(key="accountId")Long accountId,
			@Read(key="modelType")@Out(key="modelType")String modelType,
			@Read(key = "ec_crd",defaultValue="10")	int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){

		//根据houseId 查找帐户,方案详细信息
		PrePayOffsetVO offsetVO= prePayOffsetService.findPrePayOffsetByAccountId(accountId);

		//查找预交款帐户交易明细
		Page page = prePayOffsetService.queryPrePayTrans(accountId,pageNo,pageSize);
		 List<PrePayOffsetTransVO> list = page.getResult();

		Long totalCount = page.getTotalCount();
		setAttribute("list", list);
		setAttribute("totalRows", totalCount.intValue());
		setAttribute("prepayInfo", offsetVO);
		setAttribute("houseId", houseId);

		//如果是收费页面调用,则返回chargePreparePayDetailList.jsp页面
		if(!BlankUtil.isBlank(modelType) && modelType.equalsIgnoreCase("forChange")){
			return "chargePreparePayDetailList";
		}
		return "housePreparePayDetailList";
	}

    /**
	 * 功能描述:初始化变更方案页面
	 * @param houseId 房屋ID
	 * @param accountId 帐户Id
	 * @param refreshIframe 页面保存之后调用哪个iframe里面的refresh()来刷新页面
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-8-21
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
    public String initChangeChargeScheme(
    		@Read(key="keyword")Long houseId,
    		@Read(key="accountId")Long accountId,
    		@Read(key="refreshIframe",defaultValue="main")@Out(key="refreshIframe")String refreshIframe){

    	//根据总帐户查找其预交款帐户VO
    	PrePayAccountVO vo = subAccountService.getPrePayAccountById(accountId);
		setAttribute("accountId", accountId);
		setAttribute("currentChargeScheme", vo.getSchemeName());

		// 预交款方案
		List<PrePaySchVO> plist = prePaySchService.getAllPrePaySchList();
		setAttribute("prePaySchList", plist);

    	return "changeChargeScheme";
    }

    /**
	 * 功能描述:保存变更方案
	 * @param  refreshIframe 页面保存之后调用哪个iframe里面的refresh()来刷新页面
	 * @param prePaySchId 改变后预交款方案Id
	 * @param accountId 帐户Id
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-8-21
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
    public String changeChargeScheme(
    		@Read(key="refreshIframe",defaultValue="main")String refreshIframe,
    		@Read(key="prePaySchId")Long prePaySchId,
    		@Read(key="accountId")Long accountId){

    	PrePayAccountVO vo = subAccountService.getPrePayAccountById(accountId);
    	//进行变更方案修改
    	subAccountService.setPrePayScheme(vo.getSubAccountId(), prePaySchId);
    	this.getRequest().setAttribute("refreshIframe", refreshIframe);
    	return "onlyRefresh";
    }

    /**
     * 功能描述:初始化预交款抵冲主页面  <br>
     * @return
     * Create author:黄彦军 <br>
     * Create on:2008-9-22  <br>
     * Edit author:  <br>
     * Edit on:    <br>
     * Why:     <br>
     */
    public String initMain(){
    	//查询所有楼栋
    	List<BuildingVO> buildings= buildingService.getAllBuildingList();
    	//查询所有预交款方案
    	List<PrePaySchVO> prePaySchs= prePaySchService.getAllPrePaySchList();

    	setAttribute("buildings", buildings);
    	setAttribute("prePaySchs", prePaySchs);
    	return "main";
    }

    /**
     * 功能描述: 加载正在进行页面 <br>
     * @return
     * Create author:黄彦军 <br>
     * Create on:2008-9-23  <br>
     * Edit author:  <br>
     * Edit on:    <br>
     * Why:     <br>
     */
    public String initOngoing() {
		setAttribute("flag", "ongoing");
		return "onlyRefresh";
	}

    /**
	 * 功能描述:进行预交款抵冲 <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-9-23 <br>
	 * Edit author: <br>
	 * Edit on: <br>
	 * Why: <br>
	 */
    public void offset(){

    //	try {
    		//进行抵冲操作
        	prePayOffsetService.prePayOffset(this.getUserId());
	//	} catch (Exception e) {
	//		throw new UILayerException(e.getMessage(),BusinessException.FAILURE);
	//	}
    	this.renderText("success");
    }

    /**
     * 功能描述: 将查询条件设置到缓存中 <br>
     * @param offsetSearchVO
     * Create author:黄彦军 <br>
     * Create on:2008-9-22  <br>
     * Edit author:  <br>
     * Edit on:    <br>
     * Why:     <br>
     */
    public  void setSearchVO(@Read PrePayOffsetSearchVO offsetSearchVO ){
    	setAttribute("offsetSearchVO", offsetSearchVO, Scope.SESSION);
    }

    /**
     * 功能描述:获取缓存中的查询VO,如是没有,则创建一个  <br>
     * @return
     * Create author:黄彦军 <br>
     * Create on:2008-9-22  <br>
     * Edit author:  <br>
     * Edit on:    <br>
     * Why:     <br>
     */
    public PrePayOffsetSearchVO getSearchVO(){
    	PrePayOffsetSearchVO offsetSearchVO = (PrePayOffsetSearchVO) getAttribute("offsetSearchVO");
    	return offsetSearchVO == null ? new PrePayOffsetSearchVO() : offsetSearchVO;
    }

    /**
     * 功能描述:初始化主页面列表  <br>
     * @param currentPage
     * @param pageSize
     * @return
     * Create author:黄彦军 <br>
     * Create on:2008-9-22  <br>
     * Edit author:  <br>
     * Edit on:    <br>
     * Why:     <br>
     */
    public String initList(
    		@Read(key = "ec_p", defaultValue="1")Integer currentPage,
			@Read(key = "ec_crd", defaultValue="15") Integer pageSize){

    	String isFirst = this.getRequest().getParameter("isFirst");
    	if (!BlankUtil.isBlank(isFirst) && "true".equals(isFirst)){
    		setAttribute("list", null);
        	setAttribute("totalRows", 0);
        	return "list";
    	}

    	//从缓存中获取查询条件
    	PrePayOffsetSearchVO offsetSearchVO = getSearchVO();
    	offsetSearchVO.setPageNo(currentPage);
    	offsetSearchVO.setPageSize(pageSize);
    	String sortStr = ExtremeUtils.getOrderString(getRequest());
    	offsetSearchVO.setSortStr(sortStr);

    	//分页查询
    	Page page =prePayOffsetService.queryPrePay(offsetSearchVO);
    	Long totalCount =	page.getTotalCount();
    	setAttribute("list", page.getResult());
    	setAttribute("totalRows", totalCount.intValue());

    	return "list";
    }

    private IPrePayOffsetService prePayOffsetService;

    private IBuildingService buildingService;

    private IPrePaySchService prePaySchService;

    private ISubAccountService subAccountService;

    private ITransactionService transactionService;


	public void setTransactionService(ITransactionService transactionService) {
		this.transactionService = transactionService;
	}

	public void setSubAccountService(ISubAccountService subAccountService) {
		this.subAccountService = subAccountService;
	}

	public void setPrePaySchService(IPrePaySchService prePaySchService) {
		this.prePaySchService = prePaySchService;
	}

	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	public void setPrePayOffsetService(IPrePayOffsetService prePayOffsetService) {
		this.prePayOffsetService = prePayOffsetService;
	}

}
